/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.hs.srm.base.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.Constant;
import com.hs.srm.base.modules.sys.dao.SysDeptDao;
import com.hs.srm.base.modules.sys.entity.SysDeptEntity;
import com.hs.srm.base.modules.sys.service.SysDeptService;

@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {

	@Override
	public boolean edit(SysDeptEntity sysDeptEntity) {
		return this.updateById(sysDeptEntity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysDeptEntity> genDeptTree(List<SysDeptEntity> list) {
		Map<Long, SysDeptEntity> map = new HashMap<>();
		for(SysDeptEntity dept : list) {
			map.put(dept.getDeptId(), dept);
		}
		for(SysDeptEntity dept : list) {
			if(!ObjectUtils.isEmpty(dept.getParentId())) {
				SysDeptEntity parentDept = map.get(dept.getParentId());
				if(!ObjectUtils.isEmpty(parentDept)) {
					List<SysDeptEntity> childList = (List<SysDeptEntity>)parentDept.getList();
					if(ObjectUtils.isEmpty(childList)) {
						childList = new ArrayList<>();
					}
					childList.add(dept);
					parentDept.setList(childList);
					map.remove(dept.getDeptId());
					map.put(parentDept.getDeptId(), parentDept);
				}
			}
		}
		List<SysDeptEntity> result = new ArrayList<>();
		for(Long key : map.keySet()) {
			result.add(map.get(key));
		}
		return result;
	}

	/**
	 * 递归
	 */
	private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList) {
		for(Long deptId : subIdList) {
			List<Long> list = this.queryDetpIdList(deptId);
			if(list.size() > 0) {
				this.getDeptTreeList(list, deptIdList);
			}

			deptIdList.add(deptId);
		}
	}

	@Override
	public List<Long> getSubDeptIdList(Long deptId) {
		// 部门及子部门ID列表
		List<Long> deptIdList = new ArrayList<>();

		// 获取子部门ID
		List<Long> subIdList = this.queryDetpIdList(deptId);
		this.getDeptTreeList(subIdList, deptIdList);

		return deptIdList;
	}

	@Override
	public List<Long> queryDetpId() {
		return this.baseMapper.queryDetpId();
	}

	@Override
	public List<Long> queryDetpIdList(Long parentId) {
		return this.baseMapper.queryDetpIdList(parentId);
	}

	@Override
	@DataFilter(subDept = true, user = false)
	public List<SysDeptEntity> queryList(Map<String, Object> params) {
		List<SysDeptEntity> deptList = this.selectList(new EntityWrapper<SysDeptEntity>().orderBy("order_num")
				.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));

		for(SysDeptEntity sysDeptEntity : deptList) {
			SysDeptEntity parentDeptEntity = this.selectById(sysDeptEntity.getParentId());
			if(parentDeptEntity != null) {
				sysDeptEntity.setParentName(parentDeptEntity.getName());
			}
		}
		return deptList;
	}

	@Override
	public boolean save(SysDeptEntity sysDeptEntity) {
		sysDeptEntity.getMainBizIds();

		return this.insert(sysDeptEntity);
	}
}
